//
//  WBNetworkManager+Extension.swift
//  传智微博
//
//  Created by Pro on 2017/8/14.
//  Copyright © 2017年 liuyanwei. All rights reserved.
//

import Foundation


//MARK : - 封装新浪微博的网络请求方法

extension WBNetworkManager {

    
    /// 加载微博数据字典数组
    ///
    /// - Parameters:
    ///   - since_id: 返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。
    ///   - max_id: 返回ID小于或等于max_id的微博，默认为0。
    ///   - competion: 完成回调
    func statusList(since_id:Int64 = 0, max_id:Int64 = 0, competion:@escaping (_ list:[[String:AnyObject]]?, _ isSuccess:Bool)->()) {
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
      
        let params = ["since_id":"\(since_id)", "max_id":"\(max_id > 0 ?  max_id - 1 : 0)"]
        tokenRequest(method: .GET, URLString: urlString, parameters:params as [String : AnyObject] ) { (json, isSuccess) in
//            print(json ?? "" )
            //从statuses 获取字典数组，如果as? 失败，result = nil
            let result =  (json as AnyObject)["statuses"] as? [[String:AnyObject]]
            competion(result,isSuccess)
        }

    }



    //返回微博的未读数量
    func unreadCount(completion:@escaping (_ count: Int)->()){

        guard let uid = uid else{
            return
        }
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        let params = ["uid": uid]
        tokenRequest(URLString: urlString, parameters: params as [String : AnyObject]) { (json, isSuccess) in
            print("未读\(json ?? "")")
            let dict = json as? [String: AnyObject]
            let count = dict?["status"] as? Int
            completion(count ?? 0)

        }
    }








}
